﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
namespace BieuDoGiaVang
{
    public partial class SimpleChart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection())
                {
                    conn.ConnectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
                    SqlCommand sqlCmd = new SqlCommand();
                    sqlCmd.Connection = conn;
                    sqlCmd.CommandText = "SELECT CategoryName, COUNT(*) as ProductCount FROM Products p INNER JOIN Categories c ON c.CategoryID = p.CategoryID GROUP BY CategoryName ORDER BY CategoryName";
                    conn.Open();
                    SqlDataReader sqlReader = sqlCmd.ExecuteReader();
                    chtCategoriesProductCountBarChart.Series["Categories"].XValueMember = "CategoryName";
                    chtCategoriesProductCountBarChart.Series["Categories"].YValueMembers = "ProductCount";
                    chtCategoriesProductCountBarChart.DataSource = sqlReader;
                    chtCategoriesProductCountBarChart.DataBind();
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }            
            
        }
    }
}